package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.PrimaryKeyIdUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.system.api.RemoteUploadService;
import com.ruoyi.system.api.domain.Attachment;
import com.ruoyi.system.api.domain.AttachmentVO;
import com.ruoyi.system.mapper.AttachmentAssociatedMapper;
import com.ruoyi.system.mapper.AttachmentMapper;
import com.ruoyi.system.service.IAttachmentService;
import com.ruoyi.utils.UploadFileService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 附件Service业务层处理
 *
 * @author luogongquan
 * @date 2024-09-18
 */
@Service
public class AttachmentServiceImpl implements IAttachmentService {
    @Autowired
    private AttachmentMapper attachmentMapper;
    @Autowired
    private AttachmentAssociatedMapper attachmentAssociatedMapper;
    @Autowired
    private UploadFileService uploadFileService;
    @Autowired
    private RemoteUploadService remoteUploadService;

    /**
     * 查询附件
     *
     * @param id 附件主键
     * @return 附件
     */
    @Override
    public Attachment selectAttachmentById(Long id) {
        return attachmentMapper.selectAttachmentById(id);
    }

    /**
     * 查询附件列表
     *
     * @param attachment 附件
     * @return 附件
     */
    @Override
    public List<Attachment> selectAttachmentList(Attachment attachment) {
        return attachmentMapper.selectAttachmentList(attachment);
    }

    /**
     * 新增附件
     *
     * @param attachment 附件
     * @return 结果
     */
    @Override
    @Transactional
    public int insertAttachment(Attachment attachment) {
        attachment.setId(PrimaryKeyIdUtils.getSysIdWorker().nextId());
        attachment.setCreateTime(DateUtils.getNowDate());
        int i = attachmentMapper.insertAttachment(attachment);
       /* AttachmentAssociated attachmentAssociated = new AttachmentAssociated();
        attachmentAssociated.setId(PrimaryKeyIdUtils.getSysIdWorker().nextId());
        attachmentAssociated.setAttachmentId(attachment.getId());
        attachmentAssociated.setAssociatedId(attachment.getAssociatedId());
        attachmentAssociatedMapper.insertAttachmentAssociated(attachmentAssociated);*/
        return i;
    }

    /**
     * 修改附件
     *
     * @param attachment 附件
     * @return 结果
     */
    @Override
    public int updateAttachment(Attachment attachment) {
        return attachmentMapper.updateAttachment(attachment);
    }

    /**
     * 批量删除附件
     *
     * @param ids 需要删除的附件主键
     * @return 结果
     */
    @Override
    public int deleteAttachmentByIds(Long[] ids) {
        return attachmentMapper.deleteAttachmentByIds(ids);
    }

    /**
     * 删除附件信息
     *
     * @param id 附件主键
     * @return 结果
     */
    @Override
    public int deleteAttachmentById(Long id) {
        return attachmentMapper.deleteAttachmentById(id);
    }

    @Override
    public List<String> viewByAssociatedId(Long associatedId) {
        List<Attachment> fileIds = attachmentMapper.getFileIdByAssociatedId(associatedId);

        return fileIds.stream().map(fileId -> uploadFileService.getPathUrl(fileId.getFileId(), fileId.getName())).collect(Collectors.toList());
    }

    @Override
    @Transactional
    public Integer batchInsertAttachment(AttachmentVO vo) {
        if(ObjectUtils.isNotEmpty(vo.getDeleteAssociatedId())){
            //获取旧的文件信息，并删除
            Attachment param = new Attachment();
            param.setAssociatedId(vo.getDeleteAssociatedId());
            List<Attachment> deleteFile = attachmentMapper.selectAttachmentList(param);
            if(!deleteFile.isEmpty()){
                //删除旧的绑定记录
                attachmentMapper.deleteAttachmentByAssociatedId(vo.getDeleteAssociatedId());
            }

        }
        if(StringUtils.isNotEmpty(vo.getAttachments())){
            int i = attachmentMapper.batchInsertAttachment(vo.getAttachments());
        }
        //删除文件
        if(CollectionUtils.isNotEmpty(vo.getDeleteFileIds())){
            remoteUploadService.deleteFile(vo.getDeleteFileIds().toArray(new String[0]));
        }
        return  1;
    }

    @Override
    @Transactional
    public Boolean batchDeleteAttachment(List<Long> associatedIds) {
        if(ObjectUtils.isNotEmpty(associatedIds)){
            //获取旧的文件信息，并删除
            Attachment param = new Attachment();
            param.setAssociatedIds(associatedIds);
            List<Attachment> deleteFile = attachmentMapper.selectAttachmentList(param);
            if(!deleteFile.isEmpty()){
                //删除绑定记录
                attachmentMapper.deleteAttachmentByAssociatedIds(associatedIds);
                remoteUploadService.deleteFile(deleteFile.stream().map(Attachment::getFileId).toArray(String[]::new));
            }
        }

        return  true;
    }

    @Override
    public List<Attachment> getPictureByAssociatedId(Long associatedId) {
        List<Attachment> attachments = attachmentMapper.getFileIdByAssociatedId(associatedId);
        attachments.forEach(fileId -> {
            String pathUrl = uploadFileService.getPathUrl(fileId.getFileId(), fileId.getName());
            fileId.setUrl(pathUrl);
        });
        return attachments;
    }
}
